<%@ page import="com.tonbeller.jpivot.tags.OlapModelProxy" %>
<%@ page import="com.tonbeller.wcf.form.FormComponent" %>
<%@ page import="es.silenus.detecta.controller.PivotController" %>
<%@ page import="es.silenus.detecta.model.User" %>
<%@ page import="es.silenus.detecta.olap.PivotSettings" %>
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ page import="org.springframework.context.MessageSource" %>
<%@ page import="java.util.Calendar" %>
<%@ page session="true" contentType="text/html;" autoFlush="true" %>
<%@ taglib uri="http://www.tonbeller.com/jpivot" prefix="jp" %>
<%@ taglib uri="http://www.tonbeller.com/wcf" prefix="wcf" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%
	final Log LOG = LogFactory.getLog(PivotController.class);

	long start = System.currentTimeMillis();
	long end = 0;
	int year = Calendar.getInstance().get(Calendar.YEAR);

	String pivotId = (String)request.getAttribute("pivotId");
	if(pivotId == null) {
		pivotId = request.getParameter("pivotId");
	}
	final String requestURI = (String)request.getAttribute("requestURI");
	final PivotSettings settings = (PivotSettings)session.getAttribute(pivotId);
	final MessageSource messageSource = (MessageSource)request.getAttribute("messageSource");

	final String mondrianQueryId = "query" + pivotId;

	final User user = (User)request.getUserPrincipal();
	boolean isAdministrator = user.isAdmin();

	final String pivotTitle = messageSource.getMessage(settings.getTitle(), null, settings.getTitle(), user.getLocale());
	final String filenamePre = messageSource.getMessage(settings.getPrintFile(), null, settings.getPrintFile(), user.getLocale());

	OlapModelProxy theQuery = (OlapModelProxy)session.getAttribute("query"+pivotId);

	final String queryId = "#{query" + pivotId + "}";
	final String tableId = "#{table" + pivotId + "}";
	final String printId = "#{print" + pivotId + "}";

	final String naviVisible = "#{navi" + pivotId + ".visible}";
	final String mdxeditVisible = "#{mdxedit" + pivotId + ".visible}";
	final String sortformVisible = "#{sortform" + pivotId + ".visible}";
	final String levelStyle = "#{table" + pivotId + ".extensions.axisStyle.levelStyle}";
	final String hideSpans = "#{table" + pivotId + ".extensions.axisStyle.hideSpans}";
	final String showProperties = "#{table" + pivotId + ".rowAxisBuilder.axisConfig.propertyConfig.showProperties}";
	final String nonEmptyPressed = "#{table" + pivotId + ".extensions.nonEmpty.buttonPressed}";
	final String swapAxesPressed = "#{table" + pivotId + ".extensions.swapAxes.buttonPressed}";
	final String drillMemberPressed = "#{table" + pivotId + ".extensions.drillMember.enabled}";
	final String drillPositionPressed = "#{table" + pivotId + ".extensions.drillPosition.enabled}";
	final String drillReplacePressed = "#{table" + pivotId + ".extensions.drillReplace.enabled}";
	final String drillThroughPressed = "#{table" + pivotId + ".extensions.drillThrough.enabled}";
	final String printFormVisible = "#{printform" + pivotId + ".visible}";
%>
<%-- Definitions of invisible components --%>
<c:set var="pivotId" value="<%=pivotId%>"/>
<c:set var="filenamePre" value="<%=filenamePre%>"/>
<% if(theQuery == null) { %>
<jp:mondrianQuery
		id="<%=mondrianQueryId%>"
		dataSource="<%=settings.getDataSource()%>"
		dynResolver="<%=settings.getSchemaProcessor()%>"
    dynLocale='<%=user.getLocale().toString()%>'
    catalogUri="<%=settings.getCatalogUri()%>"
		useContentChecksum="<%=settings.isUseContentChecksum()%>" >
	<%=settings.getQuery()%>
</jp:mondrianQuery>
<% } %>
<jp:table id="table${pivotId}"     query="<%=queryId%>" />
<jp:navigator id="navi${pivotId}"  query="<%=queryId%>"  visible="false" />
<wcf:form id="mdxedit${pivotId}"   model="<%=queryId%>"  visible="false" xmlUri="/WEB-INF/jpivot/table/mdxedit.xml"   />
<wcf:form id="sortform${pivotId}"  model="<%=tableId%>"  visible="false" xmlUri="/WEB-INF/jpivot/table/sortform.xml"  />
<jp:print id="print${pivotId}" />
<wcf:form id="printform${pivotId}" model="<%=printId%>" visible="false"xmlUri="/WEB-INF/jpivot/print/printpropertiesform.xml"  />
<wcf:table id="query${pivotId}.drillthroughtable" visible="false" selmode="none" editable="true" />
<wcf:toolbar id="toolbar${pivotId}" bundle="com.tonbeller.jpivot.toolbar.resources">
	<wcf:scriptbutton id="cubeNaviButton" tooltip="toolb.cube" img="cube" model="<%=naviVisible%>" />
	<% if( isAdministrator) { %>
	<wcf:scriptbutton id="mdxEditButton" tooltip="toolb.mdx.edit" img="mdx-edit" model="<%=mdxeditVisible%>" />
	<% } %>
	<wcf:scriptbutton id="sortConfigButton" tooltip="toolb.table.config" img="sort-asc" model="<%=sortformVisible%>" />
	<wcf:separator />
	<wcf:scriptbutton id="levelStyle" tooltip="toolb.level.style" img="level-style" model="<%=levelStyle%>" />
	<wcf:scriptbutton id="hideSpans" tooltip="toolb.hide.spans" img="hide-spans" model="<%=hideSpans%>" />
	<wcf:scriptbutton id="propertiesButton" tooltip="toolb.properties" img="properties" model="<%=showProperties%>" />
	<wcf:scriptbutton id="nonEmpty" tooltip="toolb.non.empty" img="non-empty" model="<%=nonEmptyPressed%>" />
	<wcf:scriptbutton id="swapAxes" tooltip="toolb.swap.axes" img="swap-axes" model="<%=swapAxesPressed%>" />
	<wcf:separator />
	<wcf:scriptbutton id="drillMember" tooltip="toolb.navi.member" img="navi-member" model="<%=drillMemberPressed%>" radioGroup="navi" />
	<wcf:scriptbutton id="drillPosition" tooltip="toolb.navi.position" img="navi-position" model="<%=drillPositionPressed%>" radioGroup="navi" />
	<wcf:scriptbutton id="drillReplace" tooltip="toolb.navi.replace"  img="navi-replace" model="<%=drillReplacePressed%>" radioGroup="navi" />
	<wcf:scriptbutton id="drillThrough${pivotId}" tooltip="toolb.navi.drillthru" img="navi-through" model="<%=drillThroughPressed%>" />
	<wcf:separator />
	<wcf:scriptbutton id="printPropertiesButton${pivotId}" tooltip="toolb.print.config" img="print-config" model="<%=printFormVisible%>" />
	<wcf:imgbutton id="printpdf" tooltip="toolb.print" img="print" href="${pageContext.request.contextPath}/secure/pivotprint?cube=${pivotId}&type=1&filenamePre=${filenamePre}" />
	<wcf:imgbutton id="printxls" tooltip="toolb.excel" img="excel" href="${pageContext.request.contextPath}/secure/pivotprint?cube=${pivotId}&type=0&filenamePre=${filenamePre}" />
</wcf:toolbar>
<%
	theQuery = (OlapModelProxy)session.getAttribute("query"+pivotId);
	FormComponent theMdxEdit = (FormComponent)session.getAttribute("mdxedit"+pivotId);
	boolean overflow = false;
	try {
		overflow = theQuery.getResult().isOverflowOccured();
	} catch(Throwable e) {
		LOG.error("Error occured in: " + requestURI + " for user: " + user.toString() + " " , e);
	}
	end = System.currentTimeMillis();
%>
<fmt:bundle basename="es.silenus.detecta.olap.Messages">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="${pageContext.request.locale.language}" lang="${pageContext.request.locale.language}">
<head>
	<title><%= pivotTitle %></title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jpivot/base.css">
</head>
<body>
	<div class="pivot-header">
		<fmt:message key="pivot.title" />: <i><%= pivotTitle %></i>
	</div>
	<div class="pivot-body">
		<form action="<%=requestURI+"?pivotId="+pivotId%>" method="POST">
			<c:choose>
			<c:when test="<%=overflow%>">
			<h3><fmt:message key="pivot.overflow"/></h3>
			</c:when>
			<c:otherwise>
			<div class="pivot-toolbar">
				<wcf:render ref="toolbar${pivotId}"	xslUri="/WEB-INF/jpivot/toolbar/htoolbar.xsl" xslCache="true">
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<div class="pivot-navigator">
				<wcf:render ref="navi${pivotId}" xslUri="/WEB-INF/jpivot/navi/navigator.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<c:if test="<%=theMdxEdit.isVisible()%>">
			<div class="pivot-mdx-editor">
				<h3><fmt:message key="pivot.mdx.editor"/></h3>
				<wcf:render ref="mdxedit${pivotId}" xslUri="/WEB-INF/wcf/wcf.xsl" xslCache="true">
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			</c:if>
			<div class="pivot-sort">
				<wcf:render ref="sortform${pivotId}" xslUri="/WEB-INF/wcf/wcf.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<div class="pivot-print">
				<wcf:render ref="printform${pivotId}" xslUri="/WEB-INF/wcf/wcf.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<div class="pivot-slicer">
				<fmt:message key="pivot.slicer"/>:
				<wcf:render ref="table${pivotId}" xslUri="/WEB-INF/jpivot/table/mdxslicer.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<div class="pivot-table">
				<wcf:render ref="table${pivotId}" xslUri="/WEB-INF/jpivot/table/mdxtable.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			<div class="pivot-drillthru">
				<wcf:render ref="query${pivotId}.drillthroughtable" xslUri="/WEB-INF/wcf/wcf.xsl" xslCache="true" >
					<wcf:renderParam name="pivotId" value="<%=pivotId%>"/>
				</wcf:render>
			</div>
			</c:otherwise>
			</c:choose>
		</form>
	</div>
	<div class="pivot-footer">
	 <% if( isAdministrator) {%>(<%=((end-start)/1000)%> secs.)<% }%>
	</div>
</body>
</html>
</fmt:bundle>